//
//  MineViewController.m
//  AILock
//
//  Created by 谭亚明on 2020/6/17.
//  Copyright © 2020 tym. All rights reserved.
//

#import "AboutOurVC.h"
#import "TYMBaseTableView.h"
#import "WCQRCodeVC.h"
#import "ViesionDataView.h"
#import "WebViewController.h"
#import "UserService.h"
#import "UserMessageService.h"
#import "MessageVC.h"
#import "UIView+ZFFrame.h"
#import "MessageSettingVC.h"
#import "OrderHistoryVC.h"
#import "FamilyManageVC.h"
#import "MineSettingViewController.h"
#import "ShowQRcodeView.h"
#import "MineViewCell.h"
#import "UserTool.h"
#import "MagicImageView.h"
#import "UserModel.h"
#import "MineViewBtn.h"
#import "UIButton+ImageTitleSpacing.h"
#import "UIImageView+YPImage.h"
#import "Sizes.h"
#import "VersionModel.h"
#import  "RoomManagerVC.h"
#import "FamilyService.h"
@interface AboutOurVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,copy) NSArray *dataArray;

@property(nonatomic,weak)IBOutlet UIView *conView;
@property(nonatomic,weak)IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,weak) IBOutlet UILabel *vierLab;

@property(nonatomic,strong) UserService *services;
@property(nonatomic,strong) VersionModel *versionModel;
@end

@implementation AboutOurVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"关于我们"];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    self.vierLab.text = [NSString stringWithFormat:@"版本:%@",[infoDictionary objectForKey:@"CFBundleShortVersionString"]];
    
    _dataArray = @[@{@"title":@"许可协议",
                     @"iconName":@"icon_mine2"},
                   @{@"title":@"隐私政策",
                     @"iconName":@"icon_mine4"},
                   @{@"title":@"法律申明",
                     @"iconName":@"icon_falu"},
                   @{@"title":@"版本更新",
                     @"iconName":@"icon_gengxin"}
    ];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineViewCell" bundle:nil] forCellReuseIdentifier:[MineViewCell identifier]];
    
    self.conView.zf_width = ScreenWidth - 30;
    self.conView.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    self.conView.layer.cornerRadius = 10;
    self.conView.layer.shadowColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:0.5].CGColor;
    self.conView.layer.shadowOffset = CGSizeMake(0,0);
    self.conView.layer.shadowOpacity = 1;
    self.conView.layer.shadowRadius = 35;
    
    if (self.conView.zf_bottom > ScreenHeight - TabbarHeight) {
        [self.conView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-TabbarHeight - 15);
        }];
    }else{
        self.conView.zf_height = 360;
    }
    
    [self getVersionModel];
}

-(void)getVersionModel{
    TYMWeak(self);
    [self.services getFindnewversion:^(LMJBaseResponse * _Nonnull respon, VersionModel * _Nonnull version) {
        weakself.versionModel = version;
        [weakself.tableView reloadData];
    }];
}

-(UserService*)services{
    if (_services == nil) {
        _services = [[UserService alloc] init];
    }
    return _services;
}

#pragma  mark - tableView 代理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MineViewCell  cellForHeight];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[MineViewCell identifier] forIndexPath:indexPath];
    NSDictionary *dataDic = self.dataArray[indexPath.row];
    cell.titleLab.text = dataDic[@"title"];
    [cell.iconImage setImage:[UIImage imageNamed:dataDic[@"iconName"]]];
    
    if (indexPath.row == 3) {
        if ([self compareVesionWithServerVersion:self.versionModel.versionNo]) {
            cell.redView.hidden = NO;
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        WebViewController *webView = [[WebViewController alloc] init];
        webView.gotoURL = [AboutBaseUrl stringByAppendingString:@"/lockinstall/agreement.html"];
        webView.titleStr = @"许可协议";
        [self.navigationController pushViewController:webView animated:YES];
        
    }else if(indexPath.row == 1){
        WebViewController *webView = [[WebViewController alloc] init];
        webView.gotoURL = [AboutBaseUrl stringByAppendingString:@"/lockinstall/privacy.html"];
        webView.titleStr = @"隐私政策";
        [self.navigationController pushViewController:webView animated:YES];
    }else if(indexPath.row == 2){
        WebViewController *webView = [[WebViewController alloc] init];
        webView.gotoURL = [AboutBaseUrl stringByAppendingString:@"/lockinstall/legalNotices.html"];
        webView.titleStr = @"法律申明";
        [self.navigationController pushViewController:webView animated:YES];
    }else if(indexPath.row == 3){
        
        if ([self compareVesionWithServerVersion:self.versionModel.versionNo]) {
            
            ViesionDataView  *pop = [[ViesionDataView alloc]initWithFrame:self.view.frame];
            pop.blockCallBackIndex = ^{
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://apps.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id1421601183"] options:@{} completionHandler:^(BOOL success) {
                    }];
                } else {
                }
            };
            pop.data = self.versionModel;
            [pop viewShow];
        }else{
            [WHToast showMessage:@"已是最新版本" duration:2 finishHandler:nil];
        }
        
    }
}


/**
 比较两个版本号的大小
 @param version APP Store 版本号
 @return 版本号相等,返回NO;大于 返回YES
 */
- (BOOL)compareVesionWithServerVersion:(NSString *)version {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    if ([version compare:[infoDictionary objectForKey:@"CFBundleShortVersionString"] options:NSNumericSearch] == NSOrderedDescending) {
        return YES;
    }else{
        return NO;
    }
}
@end
